package com.chennan.springsource.lifecycle.lifebean;

/**
 * @author chennan
 * @createTime 2022年03月22日 14:58:00
 */
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;

//@Service
public class MyPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor,
        BeanDefinitionRegistryPostProcessor, ApplicationRunner , InitializingBean,
        BeanNameAware, BeanFactoryAware, ApplicationContextAware, DisposableBean {

    /**
     * 属于BeanDefinitionRegistryPostProcessor
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        System.out.println("BeanDefinitionRegistryPostProcessor==> postProcessBeanDefinitionRegistry 执行");
    }

    /**
     * 属于 MergedBeanDefinitionPostProcessor
     */
    @Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition rootBeanDefinition, Class<?> aClass, String s) {
        System.out.println("MergedBeanDefinitionPostProcessor==> postProcessMergedBeanDefinition 执行 ");
    }

    /**
     * 属于 ApplicationRunner
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner ===> run 执行");
    }

    /**
     * 属于 BeanFactoryAware
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware ===> setBeanFactory 执行");
    }

    /**
     * BeanNameAware
     * @param s
     */
    @Override
    public void setBeanName(String s) {
        System.out.println("BeanNameAware ===> setBeanName 执行");
    }

    /**
     * DisposableBean
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean ===> destroy 执行");
    }

    /**
     * InitializingBean
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean ===> afterPropertiesSet 执行");
    }

    /**
     * ApplicationContextAware
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("ApplicationContextAware ===> setApplicationContext 执行");
    }

    /**
     * BeanFactoryPostProcessor
     * @param configurableListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("BeanFactoryPostProcessor ===> postProcessBeanFactory 执行");

    }

    /**
     * BeanPostProcessor
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor ===> postProcessBeforeInitialization 执行");
        return bean;
    }

    /**
     * BeanPostProcessor
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor ===> postProcessAfterInitialization 执行");
        return bean;
    }

    /**
     * InstantiationAwareBeanPostProcessorAdapter
     * @param beanClass
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessorAdapter ===> postProcessBeforeInstantiation 执行");
        return null;
    }

    /**
     * InstantiationAwareBeanPostProcessorAdapter
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessorAdapter ===> postProcessAfterInstantiation 执行");
        return true;
    }

    /**
     * InstantiationAwareBeanPostProcessorAdapter
     * @param pvs
     * @param pds
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("InstantiationAwareBeanPostProcessorAdapter ===> postProcessPropertyValues 执行");
        return pvs;
    }

}


